//
//  SettingViewController.m
//  meitu
//
//  Created by Liccon Chang on 12-6-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SettingViewController.h"
#import "Instance.h"
#import "MarkListViewController.h"

static const int KUISwitchHeight=27;
static const int KUISwitchWidth=94;

//{{0, 0}, {125, 30}}

@implementation SettingViewController

- (void)loadView 
{
	[super loadView];
    self.navigationItem.title=@"设置";
	CGRect tableFrame = self.view.frame;
	tableFrame.origin.x = 0;
	tableFrame.origin.y = 0;
	mainTable_ = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
	mainTable_.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
	mainTable_.delegate = self;
	mainTable_.dataSource = self;
	mainTable_.backgroundColor = [UIColor clearColor];
	[self.view addSubview:mainTable_];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)handlepageSetValueChanged:(UISwitch*)sender
{
    
}

- (void)handlenightSetValueChanged:(UISwitch*)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"sendChangeNightMode" object:nil];
}

- (void)handleFullScreenSetValueChanged:(UISwitch*)sender
{
    INSTANCE.setting.iFullScreenRead=sender.on;
    [INSTANCE.setting save];
}

- (void)segmentAction:(UISegmentedControl*)sender
{
    INSTANCE.setting.iFontSizeIndex=sender.selectedSegmentIndex;
    [INSTANCE.setting save];
}

#pragma mark -
#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
	return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	switch (section) 
    {
		case 0:
			return 1;
			break;
		case 1:
			return 1;
			break;
        case 2:
			return 1;
			break;
	}
	return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	return 44.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
	static NSString *CellIdentifier = @"SettingTableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
		cell.accessoryType = UITableViewCellAccessoryNone;
    }
	int section = [indexPath section];
	int row = [indexPath row];

	switch (section) 
	{
		case 0:
			{
				switch (row) 
				{
					case 0:
						{
                            if ([LoginController hasLogin])
                            {
                                cell.textLabel.text = [NSString stringWithFormat: @"当前账号: %@",INSTANCE.contextData.userName];
                                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                                UIButton* logoutBtn=[UIButton buttonWithType:UIButtonTypeCustom];
                                [logoutBtn setFrame:CGRectMake(230, 4, 50, 36)];
                                [logoutBtn setImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
                                [logoutBtn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
                                cell.accessoryView=logoutBtn;
                            }
                            else
                            {
                                cell.textLabel.text = @"账号登陆";
                                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                                cell.accessoryView=nil;
                            }
						}
						break;
				}
			}
			break;
        case 1:
            {
                switch (row) 
                {
                    case 0:
                    {
                        cell.textLabel.text = @"收藏内容";
                        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    }
                        break;
                }
            }
			break;
		case 2:
			{
				switch (row) 
					{
					case 0:
						{
							cell.textLabel.text = @"阅览字体";
                            cell.selectionStyle = UITableViewCellSelectionStyleNone;
                            NSArray *buttonNames = [NSArray arrayWithObjects:@"小", @"中", @"大", nil];
                            UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
                            segmentedControl.tag = 3;
                            [segmentedControl setFrame:CGRectMake(130, 4, 150, 36)];
                            
                            segmentedControl.selectedSegmentIndex = 1;
                            if (INSTANCE.setting.iFontSizeIndex == 0) {
                                segmentedControl.selectedSegmentIndex = 0;
                            }
                            else if (INSTANCE.setting.iFontSizeIndex == 1){
                                segmentedControl.selectedSegmentIndex = 1;
                            }
                            else {
                                segmentedControl.selectedSegmentIndex = 2;
                            }
                            
                            //添加事件
                            [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
                            //[cell addSubview:segmentedControl];
                            cell.accessoryView=segmentedControl;
                            [segmentedControl release];
						}
						break;
//					case 1:
//						{
//							cell.textLabel.text = @"翻页功能";
//                            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//							if(pageSetSwitch_==nil)
//							{
//								CGRect rect=CGRectMake(cell.bounds.size.width-KUISwitchWidth-30, (cell.bounds.size.height-KUISwitchHeight)/2, KUISwitchWidth, KUISwitchHeight);
//								pageSetSwitch_=[[UISwitch alloc] initWithFrame:rect];
//								//pageSetSwitch_.on=Config.setting.iShowPortrait;
//								[pageSetSwitch_ addTarget:self action:@selector(handlepageSetValueChanged:) forControlEvents:UIControlEventValueChanged];
//							}
//							//[cell.contentView addSubview:pageSetSwitch_];
//                            cell.accessoryView=pageSetSwitch_;
//						}
//						break;	
//					case 2:
//						{
//							cell.textLabel.text = @"夜间模式";
//                            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//							if(nightSetSwitch_==nil)
//							{
//								CGRect rect=CGRectMake(cell.bounds.size.width-KUISwitchWidth-30, (cell.bounds.size.height-KUISwitchHeight)/2, KUISwitchWidth, KUISwitchHeight);
//								nightSetSwitch_=[[UISwitch alloc] initWithFrame:rect];
//								nightSetSwitch_.on=INSTANCE.setting.iFullScreenRead;
//								[nightSetSwitch_ addTarget:self action:@selector(handlenightSetValueChanged:) forControlEvents:UIControlEventValueChanged];
//							}
//							//[cell.contentView addSubview:nightSetSwitch_];	
//                            cell.accessoryView=nightSetSwitch_;
//						}
//						break;
                            
                            /*
                    case 1:
						{
							cell.textLabel.text = @"全屏阅读";
                            cell.selectionStyle = UITableViewCellSelectionStyleNone;
							if(fullScreenSetSwitch_==nil)
							{
								CGRect rect=CGRectMake(cell.bounds.size.width-KUISwitchWidth-30, (cell.bounds.size.height-KUISwitchHeight)/2, KUISwitchWidth, KUISwitchHeight);
								fullScreenSetSwitch_=[[UISwitch alloc] initWithFrame:rect];
								fullScreenSetSwitch_.on=INSTANCE.setting.iFullScreenRead;
								[fullScreenSetSwitch_ addTarget:self action:@selector(handleFullScreenSetValueChanged:) forControlEvents:UIControlEventValueChanged];
							}
							//[cell.contentView addSubview:fullScreenSetSwitch_];	
                            cell.accessoryView=fullScreenSetSwitch_;
						}
                        break;
                             */
					}
			}
			break;
	}
	
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	[tableView deselectRowAtIndexPath:indexPath animated:FALSE];
	int section = [indexPath section];
	int row = [indexPath row];
	switch (section) 
	{
		case 0:
			switch (row) 
			{
				case 0:
					{
                        //BaokuViewController *bk = [[BaokuViewController alloc] init];
                        //[self presentModalViewController:bk animated:YES];
                        //[bk release];
                        if(!loginController_)
                        {
                            loginController_=[[LoginController alloc] init];
                            loginController_.delegate=self;
                        }
                        [loginController_ login];
					}
					break;
			}
			break;
		case 1:
			switch (row) 
            {
                case 0:
                {
                    MarkListViewController* markCtrl=[[MarkListViewController alloc] init];
                    markCtrl.hidesBottomBarWhenPushed=TRUE;
                    [self.navigationController pushViewController:markCtrl animated:TRUE];
                    [markCtrl release];
                }
                break;
            }
			break;
	}
}

-(void)logout
{
    [INSTANCE.contextData reset];
    [mainTable_ reloadData];
}

#pragma mark -
#pragma mark LoginControllerDelegate
-(void)loginController:(LoginController*)loginController success:(BOOL)success
{
    if(success)
    {
        [mainTable_ reloadData];
    }
}

- (void)dealloc {
	[pageSetSwitch_ release];
	[nightSetSwitch_ release];
	[fullScreenSetSwitch_ release];
	[mainTable_ release];
    loginController_.delegate=nil;
    [loginController_ release];
    [super dealloc];
}


@end
